﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using Kelper.Domain.Data.Abstract;
using Kelper.Domain.Models;


namespace Kelper.Domain.Data
{
    public class EmailConfirmation : IRegistrationConfirmation
    {
        /// <summary>
        /// Ugly code, imposible to test?
        /// Hard to unit test, but it easy to test the user repository using a
        /// mock of the IRegistrationConfirmation interface
        /// Sends a confirmation email from no.reply.kelper@gmail.com 
        /// to the user
        /// </summary>
        /// <param name="user">User object to recieve the email</param>
        public void SendConfirmation(User user)
        {
            SmtpClient client = new SmtpClient();

            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.EnableSsl = true;

            MailAddress from = new MailAddress("no.reply.kelper@gmail.com", "Kelper Administration Team");
            MailAddress to = new MailAddress(user.Email, user.Username);

            MailMessage message = new MailMessage(from, to);
            message.Body = "The username " + user.Username + " has successfully been registered!";
            message.Subject = "Registration confirmation";

            NetworkCredential nc = new NetworkCredential("no.reply.kelper@gmail.com", "@dminKelper");

            client.Credentials = nc;

            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception is: " + ex);
            }
        }
    }
}
